package com.jianyun.jyzs.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.jianyun.jyzs.R;
import com.jianyun.jyzs.bean.EnterInfo;
import com.jianyun.jyzs.dao.LoginDao;

import java.util.List;

/**
 * Created by liml on 17/6/21.
 */

public class SimpleAdapter extends BaseAdapter{
    private Context context;
    private List<EnterInfo> listData;
    private LoginDao dao;

    public SimpleAdapter(LoginDao dao , Context context, List<EnterInfo> list) {
        this.dao = dao;
        this.context = context;
        this.listData = list;
    }


    @Override
    public int getCount() {
        return listData==null?0:listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        UserViewHolder mHolder = null;
        if(convertView == null) {
            convertView = View.inflate(context, R.layout.popwindow_item_delete, null);
            mHolder = new UserViewHolder();
            mHolder.tvText = (TextView) convertView.findViewById(R.id.username_item_text);
            mHolder.ivDel = (ImageView) convertView.findViewById(R.id.delete);
            convertView.setTag(mHolder);
        } else {
            // 取出holder类
            mHolder = (UserViewHolder) convertView.getTag();
        }

        // 给mHolder类中的对象赋值.
        final EnterInfo enterInfo = listData.get(position);
        mHolder.tvText.setText(enterInfo.getAccount()+"("+enterInfo.getEnterinfo()+")");
        mHolder.ivDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listData.remove(position);
                dao.delEnter(enterInfo);
                notifyDataSetChanged();
            }
        });
        return convertView;
    }
    class UserViewHolder {
        public TextView tvText;
        public ImageView ivDel;
    }
}
